﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.BaseLibrary.Tools.TypeTools;
using SystemCenterSoa.DtoModel.SystemManager;
using SystemCenterSoa.IRepository.SystemManager;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemManager
{
    //WebSystemMenuRepository
    public class WebSystemMenuRepository : BaseRepository, IWebSystemMenuRepository
    { 
        public  WebSystemMenuRepository(SystemCenterDbSoaContext SystemCenterDbSoaContext) : base(SystemCenterDbSoaContext)
        {

        }
        /// <summary>
        /// 显示全部，获取所有数据
        /// </summary>
        /// <returns></returns>
        public List<WebSystemMenuPo> GetWebSystemMenuPoAll()
        {
            //这里的排序是根据里头的排序字段来进行排序的
            return _systemCenterDbSoaContext.WebSystemMenuPo.OrderBy(p => p.wm_order).ToList();
        }
        /// <summary>
        /// 根据code进行获取
        /// </summary>
        /// <param name="wMCode"></param>
        /// <returns></returns>
        public WebSystemMenuPo GetWebSystemMenuPoByCode(string wMCode)
        {
            return _systemCenterDbSoaContext.WebSystemMenuPo.FirstOrDefault(p => p.wm_code == wMCode);
        }

        /// <summary>
        /// 根据用户名获取
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<WebSystemMenuPo> GetWebSystemMenuPosUserName(string userName)
        {
            //三表联查，因为用户名比较远，所以需要三个表进行联查获取
            return (from wsm in _systemCenterDbSoaContext.WebSystemMenuPo
                    join rmr in _systemCenterDbSoaContext.RoleModuleRelationPo on wsm.wm_module_code equals rmr.module_code
                    join aurr in _systemCenterDbSoaContext.AdminUserRoleRelationPo on rmr.role_id equals aurr.role_id
                    where aurr.user_name == userName
                    select wsm
                     ).ToList();
        }
        /// <summary>
        /// 获取父菜单的名称
        /// </summary>
        /// <param name="wMCode"></param>
        /// <returns></returns>
        public WebSystemMenuDto GetWebSystemMenuDtoByCode(string wMCode)
        {
            //sonwsm 子模块  parentWsm 父模块
            var view = (from sonWsm in _systemCenterDbSoaContext.WebSystemMenuPo
                        join parentWsm in _systemCenterDbSoaContext.WebSystemMenuPo
                        on sonWsm.wm_parent_code equals parentWsm.wm_code into leftJoinPwsm   //把前面的join into到 leftJoinPwsm，就是一个左连接
                        from leftJoinParent in leftJoinPwsm.DefaultIfEmpty()   //把join查询一下, 如果他是是有null的, 就默认返回为null。这里把内连接改为左连接
                        where sonWsm.wm_code == wMCode
                        select new { pname = leftJoinParent.wm_name, sonWsm }).FirstOrDefault();
            //这里给他判断一下，有没有数据，没有就是没有这个人了，所以返回null，有的话就接着执行
            if (view == null) return null;
            //复制其中的数据CreateInstanceFrom 这是ModelCopier里头的一个方法，是复制数据的
            WebSystemMenuDto wsmView = ModelCopier.CreateInstanceFrom<WebSystemMenuDto>(view.sonWsm);
            wsmView.ParentName = view.pname;
            return wsmView;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="wsm"></param>
        /// <returns></returns>
        public bool WebSystemMenuPoAdd(WebSystemMenuPo wsm)
        {
            _systemCenterDbSoaContext.WebSystemMenuPo.Add(wsm);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
            //这个大于0，是因为他现在是布尔类型，大于0表示是true。
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="wsm"></param>
        /// <returns></returns>
        public bool WebSystemMenuPoUpdate(WebSystemMenuPo wsm)
        {
            WebSystemMenuPo db = _systemCenterDbSoaContext.WebSystemMenuPo.FirstOrDefault(c => c.wm_code == wsm.wm_code);

            ModelCopier.CopyModel(wsm, db, "creator_name", "creator_date");
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
    }
}
